<template>
	<u-popup v-model="moreshow" mode="right" @close="close" width="52%" :customStyle="customStyle" z-index="1000">
		<view class="gpt_menu">
			<view class="content">
				<view class="add_view" @click="add_msg">
					<image src="@/static/images/add.png"></image>
					<text>新对话</text>
				</view>
				<scroll-view scroll-y class="scroll_view">
					<view class="list">
						<view class="title">今天</view>
						<view class="item" v-for="item in list" :key="item.id" @click="change(item)">
							<view class="name" v-if="edit_id!=item.id">{{item.title}}</view>
							<input type="text" v-model="item.title" focus class="name" @blur="edit_submit(item)"
								v-else />
							<view class="oper">
								<view class="icon" @click.stop="edit_click(item)" v-if="edit_id!=item.id">
									<image src="@/static/images/gpt_edit.png"></image>
								</view>
								<!-- <view class="icon" @click.stop="edit_submit(item)" v-else>
									<uv-icon name="checkbox-mark" size="20"></uv-icon>
								</view> -->
								<view class="icon" @click.stop="delete_group(item.id)">
									<image src="@/static/images/gpt_delete.png"></image>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
				<view class="menu_footer">
					<view class="clear" @click="clear_msg">清空所有聊天记录</view>
					<!-- <view class="integral">
						<view class="balance">
							<text class="key">我的对话次数：</text>
							<text class="value">{{integral_val}}</text>
						</view>
					</view> -->
					<view class="oper">
						<view class="item" @click="reLoad">
							<view class="icon">
								<image src="@/static/images/gpt1.png"></image>
							</view>
							<text>刷新</text>
						</view>
						<view class="item" @click="router">
							<view class="icon">
								<image src="@/static/images/gpt2.png"></image>
							</view>
							<text>充值</text>
						</view>
					</view>
				</view>
			</view>
		</view>
	</u-popup>
</template>

<script>
	export default {
		props: {
			model_id: {
				type: [String, Number],
				default: ""
			}
		},
		name: "GptMenu",
		data() {
			return {
				moreshow: false,
				customStyle: {
					backgroundColor: "rgba(0,0,0,0)"
				},
				integral_val: "",
				list: [],
				edit_id: ""
			};
		},
		mounted() {

		},
		methods: {
			show() {
				console.log(111);
				this.getList()
				this.moreshow = true;
			},
			async getList() {
				let {
					group,
					integral
				} = await this.$u.api.chatGroup({
					chat_model_id: this.model_id
				});
				this.list = group;
				this.integral_val = integral;
			},
			getsou() {
				uni.showToast({
					title: '点击此处',
					icon: 'success'
				})
			},
			close() {
				this.moreshow = false;
			},
			async add_msg() {
				let {
					group_id,
					group_all
				} = await this.$u.api.chatGroupNew({
					chat_model_id: this.model_id
				});
				this.$u.toast('操作成功');
				let dataItem = group_all.find(item => item.id == group_id);
				this.$emit("change", dataItem);
				this.getList();
			},
			edit_click(item) {
				this.edit_id = item.id;
			},
			async edit_submit(item){
				await this.$u.api.chatGroupEdit({
					id: this.edit_id,
					name: item.title
				});
				this.$u.toast("操作成功");
				this.edit_id = "";
			},
			async delete_group(id){
				await this.$u.api.chatGroupDel({
					id
				});
				this.$u.toast("操作成功");
				this.getList();
				this.$emit("delete", id);
			},
			async clear_msg(){
				await this.$u.api.chatGroupEmpty();
				this.$u.toast("操作成功");
				this.list = [];
				this.$emit("change");
			},
			async reLoad() {
				await this.getList();
				this.$u.toast("操作成功");
			},
			router() {
				this.navrouter("/pages/TPage/VIpServices");
			},
			change(item){
				if (this.edit_id) return false;
				this.$emit("change", item);
			}
		}
	}
</script>

<style scoped lang="scss">
	.gpt_menu {
		width: 400rpx;
		height: 100vh;
		padding-top: $navBarHeight;
		box-sizing: border-box;

		.content {
			height: calc(100vh - #{$navBarHeight});
			padding: 30rpx;
			box-sizing: border-box;
			background: #fff;

			.add_view {
				width: 100%;
				height: 68rpx;
				border-radius: 5rpx;
				border: 2rpx solid #CCCCCC;
				box-sizing: border-box;
				display: flex;
				justify-content: center;
				align-items: center;

				image {
					width: 40rpx;
					height: 40rpx;
				}

				text {
					color: #333333;
					font-size: 28rpx;
					font-weight: 400;
					padding-left: 5rpx;
					box-sizing: border-box;
				}
			}

			.scroll_view {
				height: calc(100vh - #{$navBarHeight} - 60rpx - 68rpx - 250rpx);
				padding: 20rpx 0;
				box-sizing: border-box;

				.list {
					.title {
						height: 76rpx;
						line-height: 76rpx;
						color: #000;
						font-size: 28rpx;
						font-weight: bold;

					}

					.item {
						height: 68rpx;
						display: flex;
						border-radius: 5rpx;
						margin-bottom: 20rpx;
						padding: 0 14rpx 0 24rpx;
						border-radius: 10rpx;
						box-sizing: border-box;
						background: #ECF0FF;
						display: flex;
						justify-content: space-between;
						align-items: center;

						.name {
							flex: 1;
							color: #3E65E5;
							font-size: 28rpx;
							font-weight: 400;
							white-space: nowrap;
							text-overflow: ellipsis;
							overflow: hidden;

							&:focus {
								border: none;
								outline: none;
							}
						}

						.oper {
							display: flex;

							.icon {
								margin-left: 22rpx;

								image {
									width: 40rpx;
									height: 40rpx;
								}
							}
						}
					}
				}
			}

			.menu_footer {
				height: 250rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				.clear {
					height: 60rpx;
					color: #666666;
					font-size: 24rpx;
					font-weight: 400;
					border-radius: 5rpx;
					border: 2rpx solid #CCCCCC;
					display: flex;
					justify-content: center;
					align-items: center;
				}

				.integral {
					height: 60rpx;
					border-radius: 10rpx;
					padding: 0 10rpx;
					box-sizing: border-box;
					background: #3E65E5;
					display: flex;
					align-items: center;



					.balance {
						display: flex;
						align-items: center;
						padding-left: 12rpx;
						box-sizing: border-box;

						.key {
							color: #FFFFFF;
							font-size: 26rpx;
						}

						.value {
							color: #FFFFFF;
							font-size: 32rpx;
						}
					}
				}

				.oper {
					display: flex;
					justify-content: space-between;

					.item {
						width: 160rpx;
						height: 60rpx;
						border-radius: 10rpx;
						border: 2rpx solid #CCC;
						padding-left: 14rpx;
						padding-right: 30rpx;
						box-sizing: border-box;
						background-color: #F8F8F8;
						display: flex;
						justify-content: space-between;
						align-items: center;

						.icon {
							width: 40rpx;
							height: 40rpx;

							image {
								width: 100%;
								height: 100%;
							}
						}

						text {
							color: #333;
							font-size: 28rpx;
						}
					}
				}
			}
		}
	}
</style>